<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
ini_set("memory_limit","20M");
require_once "track.inc.php";
require_once "database.inc.php";

$dbh = connect_db();

$globalactive = 0;
$globalusers = mysql_query("SELECT sum(activeusers) as active, sum(waitingusers) as waiting, sum(activeusers)+sum(waitingusers) as total FROM leagues_daily WHERE leagueid != 255", $dbh);
while ($globaluser = mysql_fetch_array($globalusers)) {
	$globalactive = $globaluser['active'];
}

$i = 0;
$hardtogetleagues = mysql_query("SELECT leagues_daily.leagueid as leagueid, tlds.tld as tld, tlds.ciatld as cia, englishname, shortname, season, activeusers, waitingusers, numberofonline, numberoflevels FROM `leagues_daily`, `tlds` where leagues_daily.leagueid = tlds.leagueid AND leagues_daily.leagueid != 255 ORDER BY activeusers ASC, season DESC, waitingusers ASC, numberofonline ASC", $dbh);
#$lastupdate = mysql_result(mysql_query("SELECT max(start) FROM scans", $dbh),0);
#echo "#last update: $lastupdate\n";
echo "#LeagueID;Coolness;Rank\n";
while ($hardtogetleague = mysql_fetch_array($hardtogetleagues)) {
	$i++;
	if ($hardtogetleague['activeusers'] > 0) {
		$coolness = $globalactive / $hardtogetleague['activeusers'];
	} else {
		$coolness = "infinite";
	}
	echo "{$hardtogetleague['leagueid']};$coolness;$i\n";
}
